<?php

require '../init.inc.php';

$buzz = '';
if (isset ($_GET['buzz'])):

        $pages = 4;
        $feed = new SimplePie;
        $feed->enable_cache(false);
        $buzz = htmlentities(strip_tags($_GET['buzz']));

        for ($i = 1; $i <= $pages; $i++):

                $feedurl = str_replace('{buzz}', $buzz, TWITTER_SEARCH);
                $feedurl = str_replace('{items}', 100, $feedurl);
                $feedurl = str_replace('{page}', $i, $feedurl);
                $urls[]  = $feedurl; 

        endfor;

        $feed->set_feed_url($urls);
        $feed->init();
        $feed->handle_content_type();

        foreach ($feed->get_items() as $item):
                $statsdate = $item->get_date('ymd');
                if (isset($stats[$statsdate])):
                        $stats[$statsdate] = $stats[$statsdate] + 1;
                else:
                        $stats[$statsdate] = 1;
                endif;
        endforeach;
        $stats = array_reverse($stats);

        $chart = new googChart();
        $chart->setChartAttrs( array(
            'type' => 'bar-vertical',
            'data' => $stats,
            'size' => array( 400, 400 )
            ));

endif;

?><!DOCTYPE html>
<html>
<head>

        <title>Testing the data mining</title>

</head>
<body>

<h1>Buzzsearch</h1>

<h2>Search buzz on twitter.com</h2>

<form method="get" action="">
        <fieldset>
                <legend>Topic</legend>
                <p>
                        <label for="buzz">Buzz topic</label>
                        <input type="text" name="buzz" id="buzz" value="<?php echo $buzz; ?>" />
                </p>
                <p>
                        <input type="submit" value="Search buzz!" />
                </p>
        </fieldset>
</form>

<?php if (isset($stats)): ?>
<h2>Amount of tweets mentioning &#8222;<?php echo $buzz; ?>&#8220; in the last days (by day ascending)</h2>

<?php echo $chart; ?>

<?php Util::printr($stats); ?>

<?php endif; ?>

</body>
</html>